package org.jgs1904.practice;

/**
 * @author: 默苍璃
 * @date: 2022-09-06 17:28
 * <p>
 * 计算前N个自然数的和
 */
public class SumOfInteger {

    public static void main(String[] args) {
        int n = 10;
        // TODO: sum = 1 + 2 + ... + n
        int sum = 0;

        //循环
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        System.out.println(sum);


        //根据公式
        sum = ((1 + n) * n) / 2;
        System.out.println(sum);

    }

}
